﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using FanaticRock.MediaSource.AutoBuilder;
using FanaticRock.MediaSource.BilibiliAPI.Extensions;
using FanaticRock.MediaSource.BilibiliAPI.Helpers;
using FanaticRock.MediaSource.BilibiliAPI.RequestVo.Fav.Folder.Created;
using FanaticRock.MediaSource.BilibiliAPI.RequestVo.WebInterface;
using FanaticRock.MediaSource.BilibiliAPI.ResponseDto.Fav.Folder.Created;
using FanaticRock.MediaSource.BilibiliAPI.ResponseDto.WebInterface;

#if NET48

#endif

namespace FanaticRock.MediaSource.BilibiliAPI.API.Fav.Folder.Created
{
    public class ListAll
    {
        [MethodInclude]
        public static async Task<ListAllResponseDto> Invoke(ListAllRequestVo vo)
        {
            try
            {
                var httpClient = HttpClientHelper.GetHttpClient();

                using var requestMessage = new HttpRequestMessage(
                    method: HttpMethod.Get,
                    requestUri: $"https://api.bilibili.com/x/v3/fav/folder/created/list-all?{vo.ToQureyParameter()}");

                requestMessage.Headers.Add(name: "Cookie", value: $"SESSDATA={vo.SessData}");

                //requestMessage.Headers.Referrer = new Uri("wwww.bilibili.com");

                using var responseMessage = await httpClient.SendAsync(requestMessage);
                responseMessage.EnsureSuccessStatusCode();

                using var stream = await responseMessage.Content.ReadAsStreamAsync();
                var resultDto = await JsonSerializer.DeserializeAsync<ListAllResponseDto>(stream);
                //var str = await responseMessage.Content.ReadAsStringAsync();
                return resultDto;
            }
            catch (Exception ex)
            {
                return new ListAllResponseDto()
                {
                    Code = Enums.CodeEnum.RequestError,
                    Message = ex.ToString(),
                };
            }
        }
    }
}
